package com.example.quartzdemo.QuartzConfig;

import com.example.quartzdemo.QuartzJob.QuartzOneJob;
import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * TODO
 *
 * @ClassName QuartzOneJobConfig
 * @Author xiaojiang
 * @Date 2021/11/12 下午 16:16
 */
@Configuration
public class QuartzOneJobConfig {

    @Bean
    public JobDetail QuartzOneJobDetail(){
        return JobBuilder.newJob(QuartzOneJob.class)// QuartzOneJob 我们的业务类
                .withIdentity("DateTimeJob")//可以给该JobDetail起一个id
                //每个JobDetail内都有一个Map，包含了关联到这个Job的数据，在Job类中可以通过context获取
                .usingJobData("msg", "Hello Quartz")//关联键值对
                .usingJobData("Hello","Quartz")
                .storeDurably()//即使没有Trigger关联时，也不需要删除该JobDetail
                .build();
    }
    @Bean
    public Trigger QuartzOneJobTrigger() {
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0/15 * * * * ?");
        return TriggerBuilder.newTrigger()
                .forJob(QuartzOneJobDetail())//关联上述的JobDetail
                .withIdentity("QuartzOneJob")//给Trigger起个名字
                .withSchedule(cronScheduleBuilder)
                .build();
    }


}
